package com.bosicen.crm.dao.orders;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.bosicen.crm.dao.HibernateDao;
import com.bosicen.crm.dao.entity.Order;
import com.bosicen.crm.dao.entity.OrderDetail;
import com.bosicen.crm.tools.AppUtil;
import com.bosicen.crm.tools.Page;
/****
 * 订单操作DAO实现
 * @author Administrator
 *
 */
@Component("ordersdao")
public class OrdersDao extends HibernateDao<Order, Integer> {
	
	/****
	 * 查询单个详细订单信息
	 * @param id 订单详细ID
	 * @return
	 */
	public OrderDetail findOrderDetail(int id){
		return (OrderDetail) this.getSession().createQuery("from OrderDetail where id=?").setParameter(0, id).uniqueResult();
	}
	/***
	 * 删除单个订单信息
	 * @param id
	 * @return
	 */
	public int delOrderDetail(int id){
		return this.getSession().createQuery("delete from  OrderDetail od where od.id=?").setParameter(0, id).executeUpdate();
	}
	/***
	 * 修改单个订单信息
	 * @param od
	 */
	public void updateOrderDetail(OrderDetail od){
		OrderDetail detail=this.findOrderDetail(od.getId());
		detail.setConfirmhigh(od.getConfirmhigh());
		detail.setConfirmlength(od.getConfirmlength());
		detail.setConfirmwidth(od.getConfirmwidth());
		detail.setDeliverydate(od.getDeliverydate());
		detail.setExdeliverydate(od.getExdeliverydate());
		detail.setNumber(od.getNumber());
		detail.setOriginalhigh(od.getOriginalhigh());
		detail.setOriginallength(od.getOriginallength());
		detail.setOriginalwidth(od.getOriginalwidth());
		detail.setPrice(od.getPrice());
		detail.setProductid(od.getProductid());
		detail.setProductno(od.getProductno());
		detail.setType(od.getType());
		this.getSession().update(detail);
	}
	
	/***
	 * 订单查询信息
	 * @param order
	 * @param page
	 * @return
	 */
	public Page findOrder(Order order,Page page){
		Map<String, Object> map=new HashMap<String,Object>();
		StringBuffer sb=new StringBuffer();
		sb.append(" from Order o where o.companyid=:companyid");
		map.put("companyid", order.getCompanyid());
		
		if(order.getSource()>0){//来源  经销商id
		   sb.append(" and o.source=:source ");
		   map.put("source", order.getSource());
		}
		
		if(order.getStatus()>0){//订单环节状态  ，就是订单目前所处环节
			sb.append(" and o.status=:status ");
			map.put("status", order.getStatus());
		}
		
		if(AppUtil.ObjectToString(order.getCustomer())!=null){//客户信息
			sb.append(" and o.customer like  '%").append(order.getCustomer()).append("%'");
		}
		
		//日期查询分两种   ：0 ：下单时间      1： 预计交货时间   2：交货时间
		if(AppUtil.ObjectToString(order.getStartdate())!=null){// 下单    //查询  开始日期
			switch(order.getType()){
				case 0: sb.append(" and DATE_FORMAT(o.createdate,'%Y-%m-%d')>=:startdate");break;
				case 1: sb.append(" and DATE_FORMAT(o.exdeliverydate,'%Y-%m-%d')>=:startdate"); break;
				case 3: sb.append(" and DATE_FORMAT(o.deliverydate,'%Y-%m-%d')>=:startdate"); break;
			}
			map.put("startdate", order.getStartdate());
		}
		
		if(AppUtil.ObjectToString(order.getEnddate())!=null){// 下单    //查询  开始日期
			switch(order.getType()){
				case 0: sb.append(" and DATE_FORMAT(o.enddate,'%Y-%m-%d')<=:enddate");break;
				case 1: sb.append(" and DATE_FORMAT(o.exdeliverydate,'%Y-%m-%d')<=:enddate"); break;
				case 3: sb.append(" and DATE_FORMAT(o.deliverydate,'%Y-%m-%d')<=:enddate"); break;
			}
			map.put("enddate", order.getEnddate());
		}
		return this.findPage(page, sb.toString(), map);
	}
	/***
	 * 获取当天订单数
	 * @return
	 */
	public int findOrders4day(){
		return AppUtil.ObjectToInt(this.getSession().createSQLQuery("select count(*) from Orders o where DATE_FORMAT(o.sysinsertdate,'%Y-%m-%d')=:createdate").setParameter("createdate", AppUtil.getcurrdate()).uniqueResult());
	}
	/****
	 * 通过订单id查询订单详情
	 * @param orderid
	 * @return
	 */
	public Order findOrderById(int orderid){
		return (Order) this.getSession().createQuery("from Order where id=?").setParameter(0, orderid).uniqueResult();
	}
	/***
	 * 通过订单id查询订单详细列表
	 * @param pid  订单id  
	 * @param sery 系列id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Order> findOrderDetailByPid(int pid,int seryid){

		Query q = this.getSession().createQuery(
                "from Order as o " +
                "inner join   o.orderdetail  as detail " +
                "where o.id=? and detail.type=? order by detail.id asc"
            );
		q.setParameter(0, pid);
		q.setParameter(1, seryid);
		

		return q.list();
	}
	/****
	 * 查询详细产品数量
	 * @param pid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public 	List<Object[]> findOrderDetailByType(int pid){
		List<Object[]> list=this.getSession().createSQLQuery("select type,count(*)  from order_detail where pid =? group by type ").setParameter(0, pid).list();
		return list;
	}
}
